package com.example.demo.controller;

import com.example.demo.common.Func;
import com.example.demo.common.Reptile;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/htmlunit")
@Api(tags = "4、UI自动化")
@ApiSupport(author = "罗海林", order = 4)
@Validated
@Slf4j
public class HtmlUnitController {
    @Test
    public void test1() throws Exception {
        String url = "http://ggzy.jgswj.gxzf.gov.cn/bhggzy/jyxx/001001/001001002/001001002001/tradeInfo.html";

        WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);

        HtmlPage page = webClient.getPage(url);

        DomElement inputEle = page.getFirstByXPath("//*[@id='serviceType']/li[@data-value='001001']");
        inputEle.click();

        webClient.waitForBackgroundJavaScript(2000);

        DomElement tr = page.getFirstByXPath("//*[@id='list']/tr[1]/td[1]/a");
        HtmlPage pageNew = tr.click();

        webClient.waitForBackgroundJavaScript(2000);

        HtmlInlineFrame iframe = (HtmlInlineFrame)pageNew.getElementById("iframeList");
        HtmlPage pageIframe = (HtmlPage) iframe.getEnclosedPage();

        Func.print(pageNew.getUrl().toString());

        /*Func.print(pageIframe.asXml());
        Func.print("俣哈哈哈哈哈俣");
        Func.print(pageNew.asXml());*/
    }

    @Test
    public void test2() throws Exception {
        Reptile rep = new Reptile();
        rep.open("http://ggzy.jgswj.gxzf.gov.cn/bhggzy/jyxx/001001/001001002/001001002001/tradeInfo.html");

        String today = "2023-10-01";
        rep.wait(2000);

        String[] businessArray = { "工程建设", "政府采购", "其他交易" };
        List<DomElement> businessList = rep.getDomListByXPath("//*[@id='serviceType']/li");
        for (DomElement businessli : businessList) {
            String business = rep.getText(businessli);
            for (int i = 0; i < businessArray.length; i++) {
                if (business.equals(businessArray[i])) {
                    businessli.click();
                    rep.wait(2000);

                    while (true) {
                        boolean isEnd = false;

                        List<DomElement> trList = rep.getDomListByXPath("//*[@id='list']/tr");
                        for (DomElement tr : trList) {
                            DomElement td = rep.getDomByTag(tr, new String[] { "td,1" });
                            String date = rep.getText(td);

                            if (date.compareTo(today) > -1) {
                                DomElement a = rep.getDomByTag(tr, new String[] { "td,0", "a" });
                                String title = rep.getText(a);

                                Func.print("[" + date + "] " + title);
                            }
                            else
                            {
                                isEnd = true;
                                break;
                            }
                        }

                        break;
                    }
                }
            }
        }

        rep.close();
    }

    @Test
    public void test10() {
        log.info("111111");
        log.error("alsdfasdfasdfasfasf");
        log.trace("333333");
        log.warn("4555677");
        log.debug("test");
    }
}
